<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/component/pear/css/pear.css"/>
    <script src="/static/component/layui/layui.js"></script>
    <script src="/static/component/pear/pear.js"></script>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <table id="dataTable" lay-filter="dataTable"></table>
    </div>
</div>
<script type="text/html" id="toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>
<script type="text/html" id="options">
    {{# if (d.director_audit == 1||(d.director_audit == 2 && d.finance_audit == 1)||(d.director_audit==2&&d.finance_audit==2&&d.investor_audit==1)){ }}
    {if $status}
    <button class="pear-btn pear-btn-primary pear-btn-xs" lay-event="edit">通过</button>
    <button class="pear-btn pear-btn-danger pear-btn-xs" lay-event="remove">拒绝</button>
    {/if}
    {{# } }}
    {{# if (d.investor_audit == 2 && d.is_receive == 1){ }}
<!--    <button class="pear-btn pear-btn-primary pear-btn-xs" lay-event="receive">领取</button>-->
    {{# } }}
</script>
<script>
    layui.use(['table', 'form', 'jquery', 'common', 'laydate'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let laydate = layui.laydate;
        let MODULE_PATH = "{$Request.root}/finance.assets/";

        let cols = [
            [{
                field: "id",
                title: "ID",
                unresize: "true",
                align: "center"
            }, {
                field: "cash_info",
                title: "说明",
                unresize: "true",
                align: "center"
            }, {
                field: "cash_price",
                title: "金额",
                unresize: "true",
                align: "center"
            }, {
                field: "cash_note",
                title: "备注",
                unresize: "true",
                align: "center"
            }, {
                field: "admin_id",
                title: "申请人",
                unresize: "true",
                align: "center", templet: d => {
                    if (d.get_admin) {
                        return d.get_admin.username
                    } else {
                        return ''
                    }
                }
            }, {
                field: "school_id",
                title: "所属学校",
                unresize: "true",
                align: "center", templet: d => {
                    if (d.get_school) {
                        return d.get_school.school_name
                    } else {
                        return ''
                    }
                }
            },
            //     {
            //     field: "group",
            //     title: "部门",
            //     unresize: "true",
            //     align: "center"
            // },
                {
                field: "create_time",
                title: "申请时间",
                unresize: "true",
                align: "center"
            },
                {
                    field: "director_audit",
                    title: "主管审批",
                    unresize: "true",
                    align: "center",templet: d =>{
                        if (d.director_audit == 2) {
                            return '通过'
                        } else if (d.director_audit == 3) {
                            return '驳回'
                        } else{
                            return  '待审核'
                        }
                    }
                },
                {
                field: "finance_audit",
                title: "财务审批",
                unresize: "true",
                align: "center",templet: d =>{
                    if (d.finance_audit == 2) {
                        return '通过'
                    } else if (d.finance_audit == 3) {
                        return '驳回'
                    } else{
                        return  '待审核'
                    }
                }
            },
                {
                field: "investor_audit",
                title: "投资人审核",
                unresize: "true",
                align: "center",templet: d =>{
                    if (d.investor_audit == 2) {
                        return '通过'
                    } else if (d.investor_audit == 3) {
                        return '驳回'
                    } else{
                        return  '待审核'
                    }
                }
            }, {
                title: '操作',
                toolbar: '#options',
                unresize: true,
                align: 'center',
                width: 180,
            }
            ]
        ]

        table.render({
            elem: '#dataTable',
            url: MODULE_PATH + 'petty_cash',
            page: true,
            cols: cols,
            cellMinWidth: 100,
            skin: 'line',
            toolbar: '#toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        table.on('tool(dataTable)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            } else if ('receive' === obj.event) {
                window.receive(obj);
            }
        });

        table.on('toolbar(dataTable)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            } else if (obj.event === 'recycle') {
                window.recycle(obj);
            }
        });

        form.on('submit(query)', function (data) {
            table.reload('dataTable', {
                where: data.field,
                page: {curr: 1}
            })

            return false;
        });

        //弹出窗设置 自己设置弹出百分比
        function screen() {
            if (typeof width !== 'number' || width === 0) {
                width = $(window).width() * 0.8;
            }
            if (typeof height !== 'number' || height === 0) {
                height = $(window).height() - 20;
            }
            return [width + 'px', height + 'px'];
        }

        window.add = function () {
            layer.open({
                type: 2,
                maxmin: true,
                title: '新增备用金申请',
                shade: 0.1,
                area: screen(),
                content: MODULE_PATH + 'add'
            });
        }

        window.receive = function (obj) {
            layer.confirm('确定已领取该备用金申请', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + 'receiveCash',
                    data: {
                        id: obj.data['id'],
                        is_receive: 2
                    },
                    dataType: 'json',
                    type: 'POST',
                    success: function (res) {
                        layer.close(loading);
                        //判断有没有权限
                        if (res && res.code == 999) {
                            layer.msg(res.msg, {
                                icon: 5,
                                time: 2000,
                            })
                            return false;
                        } else if (res.code == 200) {
                            layer.msg(res.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                table.reload('dataTable');
                            });
                        } else {
                            layer.msg(res.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.edit = function (obj) {
            layer.confirm('确定要通过该备用金申请', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + 'auditCash',
                    data: {
                        id: obj.data['id'],
                        finance_audit: 2
                    },
                    dataType: 'json',
                    type: 'POST',
                    success: function (res) {
                        layer.close(loading);
                        //判断有没有权限
                        if (res && res.code == 999) {
                            layer.msg(res.msg, {
                                icon: 5,
                                time: 2000,
                            })
                            return false;
                        } else if (res.code == 200) {
                            layer.msg(res.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                table.reload('dataTable');
                            });
                        } else {
                            layer.msg(res.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.recycle = function () {
            layer.open({
                type: 2,
                maxmin: true,
                title: '回收站',
                shade: 0.1,
                area: screen(),
                content: MODULE_PATH + 'recycle',
                cancel: function () {
                    table.reload('dataTable');
                }
            });
        }


        window.remove = function (obj) {
            layer.confirm('确定要驳回该备用金申请', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + 'auditCash',
                    data: {
                        id: obj.data['id'],
                        finance_audit: 3
                    },
                    dataType: 'json',
                    type: 'POST',
                    success: function (res) {
                        layer.close(loading);
                        //判断有没有权限
                        if (res && res.code == 999) {
                            layer.msg(res.msg, {
                                icon: 5,
                                time: 2000,
                            })
                            return false;
                        } else if (res.code == 200) {
                            layer.msg(res.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                table.reload('dataTable');
                            });
                        } else {
                            layer.msg(res.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }
            var ids = []
            var hasCheck = table.checkStatus('dataTable')
            var hasCheckData = hasCheck.data
            if (hasCheckData.length > 0) {
                $.each(hasCheckData, function (index, element) {
                    ids.push(element.id)
                })
            }
            layer.confirm('确定要删除这些备用金申请', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + 'batchRemove',
                    data: {ids: ids},
                    dataType: 'json',
                    type: 'POST',
                    success: function (res) {
                        layer.close(loading);
                        //判断有没有权限
                        if (res && res.code == 999) {
                            layer.msg(res.msg, {
                                icon: 5,
                                time: 2000,
                            })
                            return false;
                        } else if (res.code == 200) {
                            layer.msg(res.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                table.reload('dataTable');
                            });
                        } else {
                            layer.msg(res.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.refresh = function (param) {
            table.reload('dataTable');
        }
    })
</script>
</body>
</html>
